package com.zlc.atonce.huarongdao.gui;


import com.zlc.atonce.huarongdao.algorithme.HRDImpl;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Set;

public class AtOnceApplication extends Application {
    // 横刀立马
    public static final String HDLM = "EGIE,FHJF,ECDE,FBBF,BAAB";
    TextField rowColCounts = new TextField("5 4");
    TextField outSource = new TextField(HDLM);
    TextArea logMessage = new TextArea("log message");
    private MyGrid gridPane;

    /**
     * 实现在Pane内部鼠标点击自动生成Label
     */
    Parent getParent() {
        BorderPane pane = new BorderPane();

        pane.setStyle("");


        VBox top = new VBox();
        HBox top1 = new HBox();
        Button daaa = new Button("重置");
        daaa.setOnAction(e -> initGrid2());
        Button computeNext = new Button("查找下一步");
        Button ansButton = new Button("查找答案");
        HBox top2 = new HBox();

        computeNext.setOnAction(e -> {
            logMessage.clear();
            Set<String> strings = gridPane.computeNextStep();
            logMessage.appendText(String.join("\n", strings));
        });
        ansButton.setOnAction(e -> {
            Stage stage = new Stage();
            String identity = gridPane.getIdentity();
            HRDImpl hrd = HRDImpl.fromIdentity(identity);
            Scene scene = new Scene(new AnsPane(hrd.findTarget()));

            stage.setTitle("answer!");
            stage.setScene(scene);
            stage.setAlwaysOnTop(true);

            stage.show();
        });
        Button importId = new Button("导入id");
        importId.setOnAction(e -> gridPane.updateByIdentity(outSource.getText()));
        Button generateId = new Button("生成id");
        generateId.setOnAction(e -> outSource.setText(gridPane.getIdentity()));
        top1.getChildren().addAll(daaa, rowColCounts, ansButton);
        HBox.setHgrow(outSource, Priority.ALWAYS);
        top2.getChildren().addAll(outSource, importId, generateId, computeNext);
        top.getChildren().addAll(top1, top2);
        gridPane = new MyGrid();
        gridPane.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
                System.out.println(keyEvent);
                if (keyEvent.getCode() == KeyCode.SPACE) {
                    MyLabel[] selectedLabel = gridPane.getSelectedLabel();
                    MyLabel.combine(selectedLabel);
                } else if (keyEvent.getCode() == KeyCode.O) {
                    String identity = gridPane.getIdentity();
                    outSource.setText(identity);
                } else if (keyEvent.getCode() == KeyCode.I) {
                    String identity = outSource.getText();
                    gridPane.updateByIdentity(identity);
                }
            }
        });
        logMessage.setPrefWidth(415);

        pane.setTop(top);
        pane.setCenter(gridPane);
        pane.setBottom(logMessage);

        return pane;
    }

    private void initGrid2() {
        String[] split = rowColCounts.getText().split(" ");
        gridPane.initGrid(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
    }

    @Override
    public void start(Stage stage0) {
        Stage stage = new Stage();
        Scene scene = new Scene(getParent());

        stage.setTitle("train!");
        stage.setScene(scene);
        stage.setAlwaysOnTop(true);

        stage.show();
    }

}